import data_storage from '@ohos.data.storage';
import featureAbility from '@ohos.ability.featureAbility';
export default {
    data: {
        changeSelectAll: false,
        selectAllImage: '/common/icon/未选中.png',
        goodsList: [],
    },
    onInit() {
        var context = featureAbility.getContext();
        context.getFilesDir().then((filePath) => {
            let storage = data_storage.getStorageSync(filePath + '/myStore')
            let goodsList = JSON.parse(storage.getSync('goodsList',JSON.stringify([])))
            this.goodsList = goodsList
            this.judgmentSelectAll()
        });
    },
    judgmentSelectAll() {
        if (this.goodsList.length === 0) {
            this.changeSelectAll = false;
            this.toggleIcon()
            return
        }
        let i
        for (i = 0; i < this.goodsList.length; i++) {
            if (this.goodsList[i].checked === false) {
                break
            }
        }
        if (i === this.goodsList.length) {
            this.changeSelectAll = true;
        } else {
            this.changeSelectAll = false;
        }
        this.toggleIcon()
    },
    toggleIcon() {
        if (this.changeSelectAll) {
            this.selectAllImage = '/common/icon/选中.png'
        } else {
            this.selectAllImage = '/common/icon/未选中.png'
        }
    },
    toggle() {
        this.changeSelectAll = !this.changeSelectAll
        this.toggleIcon()
        this.goodsList.forEach(v => {
            v.checked = this.changeSelectAll
        })
        var context = featureAbility.getContext();
        context.getFilesDir().then((filePath) => {
            let storage = data_storage.getStorageSync(filePath + '/myStore')
            storage.putSync('goodsList', JSON.stringify(this.goodsList))
            storage.flushSync()
        })
        console.log(JSON.stringify(this.goodsList))
    },
    changeNum(e) {
        console.log(JSON.stringify(e))
        let d = e.detail
        let index = this.goodsList.findIndex(v => v.id === d.id);
        if (index >= 0 && index < this.goodsList.length) {
            if (d.num <= 0) {
                this.goodsList.splice(index,1)
                this.judgmentSelectAll()
            }else {
                this.goodsList[index].buyNum = d.num;
            }
        }
        var context = featureAbility.getContext();
        context.getFilesDir().then((filePath) => {
            let storage = data_storage.getStorageSync(filePath + '/myStore')
            storage.putSync('goodsList', JSON.stringify(this.goodsList))
            storage.flushSync()
        })
    },
    changeChecked(e) {
        console.log(JSON.stringify(e))
        let d = e.detail
        let index = this.goodsList.findIndex(v => v.id === d.id);
        if (index >= 0 && index < this.goodsList.length) {
            this.goodsList[index].checked = d.result;
        }
        var context = featureAbility.getContext();
        context.getFilesDir().then((filePath) => {
            let storage = data_storage.getStorageSync(filePath + '/myStore')
            storage.putSync('goodsList', JSON.stringify(this.goodsList))
            storage.flushSync()
        })
        this.judgmentSelectAll()
    },
    clear() {
        this.$element('dialog').show()
    },
    yes(){
        this.$element('dialog').close()
        this.goodsList = []
        var context = featureAbility.getContext();
        context.getFilesDir().then((filePath) => {
            let storage = data_storage.getStorageSync(filePath + '/myStore')
            storage.putSync('goodsList', JSON.stringify(this.goodsList))
            storage.flushSync()
        })
        this.judgmentSelectAll()
    },
    no(){
        this.$element('dialog').close()
    },
    computed: {
        totalPrice() {
            let p = 0;
            let f = false;
            this.goodsList.forEach(v => {
                if (v.checked) {
                    f = true
                    p += v.nowPrice * v.buyNum
                }
            })
            if (f) {
                p += 3
            }
            return p;
        }
    },
}
